import type { ResponsePromise } from "ky"

import type { PlasmoMessaging } from "@plasmohq/messaging"

import { chatToBaiduAI } from "~api/baiduchatAPI"
import { openAIChatApi } from "~api/openAIChatApi"
import { useTokenStroe } from "~store/tokenStore"
import type { ChatMessageType, ChatQueryType } from "~type/chatType"
import { OIAItypeObj } from "~util/AIUtil"
import { chatSSEAPIPort } from "~util/sse"

const handler: PlasmoMessaging.PortHandler<
  ChatQueryType,
  ChatMessageType
> = async (req, res) => {
  const sourcre = useTokenStroe.getState().tokenType.aiSource
  switch (sourcre) {
    case OIAItypeObj.baidu.type:
      await chatSSEAPIPort<ChatQueryType, ChatMessageType>({
        req,
        res,
        requestAPI: chatToBaiduAI
      })
      break
    case OIAItypeObj.chatgpt.type:
      await chatSSEAPIPort<ChatQueryType, ChatMessageType>({
        req,
        res,
        requestAPI: openAIChatApi
      })
      break
  }

  //
}

export default handler
